package hk.servlet;

import hk.dao.UserDao;
import hk.pojo.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version 1.0
 * @Author YangJiaQuan_hrbnu
 * @Date 2024/4/24 16:39
 */

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

    private UserDao dao = new UserDao();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String flag = request.getParameter("flag");

        if("login".equals(flag)){
            login(request,response);
        }
    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("adminName");
        String password = request.getParameter("password");

        UserInfo user = new UserInfo();
        user.setUserName(username);
        user.setPassword(password);
        UserInfo login = dao.login(user);
        PrintWriter pw =response.getWriter();
        if(login==null) {
            pw.print("0");
        }
        else {
            request.getSession().setAttribute("session_admin", login);
            pw.print("1");
        }
    }
}
